/*

            Author : Roy Raul.

            Component : XmlResource             

            Version History         : 0.1 Date Jul-20-2011

            Current Version         : 0.1 Date Jul-20-2011

            Build Date              : Apr-14-2010

*/
package com.micmac.main.mainmodel.helpers{
	public class XmlResource{
		static public function parse(data:Object, node:Object, prefix:String=''):void{
			for(var i:Number=0;i<node.childNodes.length;i++){
				var currentNode:Object = node.childNodes[i];
				if (currentNode.nodeName=='param' || currentNode.nodeName=='item'){
					if (currentNode.attributes.value!=null)
						data[(prefix+currentNode.attributes.name).toLowerCase()] = currentNode.attributes.value;
					else
						data[(prefix+currentNode.attributes.name).toLowerCase()] = currentNode.firstChild.nodeValue;		
				}
				else if (currentNode.nodeName=='group' && currentNode.hasChildNodes()){
					XmlResource.parse(data, currentNode, prefix+currentNode.attributes.name+'.');	
					continue;
				}
				else if (currentNode.nodeName=='groupArray' && currentNode.hasChildNodes()){
					data[(prefix+currentNode.attributes.name).toLowerCase()] = parseAsArray(data, currentNode);	
					continue;
				}
				if (currentNode.hasChildNodes()) XmlResource.parse(data, currentNode, prefix);
			}
		}
		
		private static function parseAsArray(data:Object, node:Object):Array{
			var tempArr:Array = new Array();
			for(var i:Number=0;i<node.childNodes.length;i++){
				var currentNode:Object = node.childNodes[i];
				if (currentNode.nodeName=='param' || currentNode.nodeName=='item'){
					var obj:Object = new Object();
					if (currentNode.attributes.value!=null){
						tempArr[currentNode.attributes.name.toLowerCase()] =  currentNode.attributes.value;
						//tempArr.push(obj);
					}
					else{
						tempArr[currentNode.attributes.name.toLowerCase()] = currentNode.firstChild.nodeValue;	
						//tempArr.push(obj);	
					}
				}
			}
			return tempArr;
		}
	}
}
